package com.pubpage.flow.flow_search;

import javax.servlet.http.HttpServletRequest;
import com.pub.database.QueryBean;
import com.pub.database.DataView;
import com.pub.database.DataViewForm;

public class Action {
    QueryBean gqbnDAO=null;
    public String action(javax.servlet.jsp.PageContext pageContext){
       HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();

       View view=(View)request.getAttribute("view");

       String lstrUserNO = null;
       gqbnDAO=new QueryBean(view.getdb_alias(),view.getdb_debug(),view.getsource_charset(),view.gettarget_charset());

       DataView DataView= new DataView(gqbnDAO);
       DataViewForm DataViewForm = (DataViewForm)request.getAttribute("DataViewForm");

       //request.setAttribute("DataViewForm",DataViewForm);

       view.setDataView(DataView);
       view.setDataViewForm(DataViewForm);

       view.setRequest_URI(request.getRequestURI());

       Business business=new Business(gqbnDAO);

       String lstrResult=null;

       try{
           if(view.getPrgAction()!=null && view.getPrgAction().equals("getData")){//利用AJAX即時讀取資料
               lstrResult=business.getData(view,lstrUserNO);

               pageContext.getOut().clear();
               pageContext.getOut().write(lstrResult);
               pageContext.getOut().flush();
               return "login";
           }

           if(view.getPrgAction()!=null && view.getPrgAction().equals("dataview_change_page")){
               business.dataViewAction(view,lstrUserNO);
           }

           business.initAction(view,lstrUserNO);

          return "";
       }
       catch(Exception e){
           if(view.getdb_debug()){
               e.printStackTrace();
           }
           view.setOnloadScript("alert('Program error please contact IT.');");
           return "";
       }
       finally{
          try{
             if(gqbnDAO!=null){
                gqbnDAO.close();
             }
          }
          catch(Exception e){
             System.out.println("can not close querybean in Action.java");
          }
       }
   }
}
